<template>
  <div>
    <el-form ref="dynamicValidateForm" :model="dynamicValidateForm" label-width="100px" class="demo-dynamic">
      <el-form-item
        prop="name"
        label="名称"
        :rules="[
          { required: true, message: '请输入名称', trigger: 'blur' },
          // { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ]"
      >
        <el-input v-model="dynamicValidateForm.name" />
      </el-form-item>
      <el-form-item
        prop="address"
        label="地址"
      >
        <el-input v-model="dynamicValidateForm.address" />
      </el-form-item>
      <el-form-item
        prop="description"
        label="描述"
      >
        <el-input v-model="dynamicValidateForm.description" />
      </el-form-item>

      <el-form-item>
        <el-select v-model="dynamicValidateForm.deviceIds" multiple placeholder="请选择">
          <el-option
            v-for="item in devices"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>

      </el-form-item>
    </el-form>
  </div>

</template>
<script>
import { addDeviceGroup, getUnbindingDevices } from '@/api/device-group'
export default {
  data() {
    return {
      dynamicValidateForm: {
        name: '',
        description: '',
        address: undefined
      },
      devices: [],
      deviceQuery: {
        productId: undefined
      }
    }
  },
  created() {
    this.getUnbinding()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addDeviceGroup(this.dynamicValidateForm).then(response => {
            this.$router.push({ path: '/device-group' })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getUnbinding() {
      getUnbindingDevices(this.deviceQuery).then(response => {
        this.devices = response.data
      })
    }

  }
}
</script>
<style lang="scss">

</style>
